package de.streblow.inventur;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class TableActivity extends Activity implements OnClickListener {

	private String teamID;
	private Button button;
	private ListView listView;
	private ArrayAdapter<String> adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_table);
		
		teamID = getIntent().getExtras().getString("teamID");
		//read entries
		File filesDir = getFilesDir();
		File fInventurfile = new File(filesDir, teamID);
		FileInputStream fisInventurfile;
		InputStreamReader isrInventurfile;
		BufferedReader brInventurfile;
		String line = "";
		ArrayList<String> lines = new ArrayList<String>();
		try {
			fisInventurfile = new FileInputStream(fInventurfile);
			isrInventurfile = new InputStreamReader(fisInventurfile);
			brInventurfile = new BufferedReader(isrInventurfile);
			while ((line = brInventurfile.readLine()) != null) {
				lines.add(line);
			}
			fisInventurfile.close();
		} catch (Exception e) {
			Messagebox.showMessagebox(this, "Error!", e.getMessage());
		}
		String[] entries = new String[lines.size()];
		for (int i = 0; i < lines.size(); i++) {
			entries[i] = lines.get(lines.size() - 1 - i);
		}
		
		listView = (ListView) findViewById(R.id.entries_list);
		button = (Button) findViewById(R.id.buttonDeleteEntries);
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, entries);
		listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		listView.setAdapter(adapter);
		button.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.action_settings:
			Intent intent = new Intent(TableActivity.this, OptionsActivity.class);
			startActivityForResult(intent, 0);
			return true;
		case R.id.action_help:
			HelpDialog help = new HelpDialog(this);
			help.setTitle(R.string.help_title);
			help.show();
			return true;
		case R.id.action_about:
			AboutDialog about = new AboutDialog(this);
			about.setTitle(R.string.about_title);
			about.show();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void onClick(View v) {
		//overwrite file teamID if there is something to delete
		SparseBooleanArray checked = listView.getCheckedItemPositions();
		if (checked.size() > 0) {
			FileOutputStream fosInventurfile;
			String line = "";
			try {
				File filesDir = getFilesDir();
				File fInventurfile = new File(filesDir, teamID);
				fInventurfile.delete();
				fosInventurfile = openFileOutput(teamID, Context.MODE_APPEND);
				for (int i = listView.getCount() - 1; i >= 0; i--) {
					if (!checked.get(i)) {
						line = adapter.getItem(i) + "\n";
						try {
							fosInventurfile.write(line.getBytes());
						} catch (IOException e) {
							Messagebox.showMessagebox(TableActivity.this, "Error!", e.getMessage());
						}
					}
				}
				fosInventurfile.close();
			} catch (Exception e) {
				Messagebox.showMessagebox(this, "Error!", e.getMessage());
			}
		}
		finish();
	}

}
